package com.liang.sql;

import java.io.Serializable;
import java.lang.reflect.Field;

public class MyBatis implements Serializable {

	private static final long serialVersionUID = 1L;

	public String tablename() {
		TableBind table = this.getClass().getAnnotation(TableBind.class);
		if (table != null)
			return table.tableName();
		else
			return this.getClass().getSimpleName();
	}

	public String id() {
		for (Field field : this.getClass().getDeclaredFields()) {
			if (field.isAnnotationPresent(Id.class))
				return field.getAnnotation(Id.class).fieldName();
		}

		throw new RuntimeException("undefine POJO @Id");
	}

	public String realId() {
		for (Field field : this.getClass().getDeclaredFields()) {
			if (field.isAnnotationPresent(Id.class))
				return field.getName();
		}
		throw new RuntimeException("undefine id");
	}
}
